package de.slothsoft.wasbinich.swingclient.control;

import java.awt.Component;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import de.slothsoft.commons.swing.GridBagUtil;
import de.slothsoft.wasbinich.core.ApplicationInfo;
import de.slothsoft.wasbinich.core.WasBinIch;
import de.slothsoft.wasbinich.core.WebserviceUrls;
import de.slothsoft.wasbinich.core.database.ApplicationBean;
import de.slothsoft.wasbinich.core.database.Database;
import de.slothsoft.wasbinich.core.database.DatabaseException;
import de.slothsoft.wasbinich.swingclient.WasBinIchSwingClient;
import de.slothsoft.wasbinich.swingclient.common.SwingUtil;
import de.slothsoft.wasbinich.swingclient.control.TitlePanel.ScreenSwitcher;
import de.slothsoft.wasbinich.swingclient.res.AndroidMessages;
import de.slothsoft.wasbinich.swingclient.res.Messages;

public class AboutPanel extends ChildPanel {

    private static final long serialVersionUID = -5644476984634609514L;

    public AboutPanel(ScreenSwitcher screenSwitcher) {
	super(screenSwitcher);
    }

    @Override
    protected Component createControls() {
	JPanel panel = new JPanel();
	panel.setOpaque(false);
	panel.setLayout(new GridBagLayout());

	ApplicationBean applicationBean = Database.getInstance().getApplicationBean();
	try {
	    panel.add(createAboutPanel(Messages.getString("AboutPanel.clientInfo"), applicationBean.getClientInfo()),
		    GridBagUtil.componentDefaults(0, 0));
	} catch (DatabaseException e) {
	    WasBinIchSwingClient.handleException(e);
	}
	try {
	    panel.add(
		    createServerAboutPanel(AndroidMessages.getDefault("info_label_server"),
			    applicationBean.getServerInfo()), GridBagUtil.componentDefaults(1, 0));
	} catch (DatabaseException e) {
	    WasBinIchSwingClient.handleException(e);
	}

	panel.add(SwingUtil.createLink(WasBinIch.VENDOR_NAME, WasBinIch.VENDOR_URL), GridBagUtil.labelDefaults(0, 1));
	panel.add(SwingUtil.createLink(WasBinIch.APPLICATION_NAME, WasBinIch.APPLICATION_URL),
		GridBagUtil.labelDefaults(1, 1));

	return panel;
    }

    private ApplicationInfoPanel createServerAboutPanel(String title, ApplicationInfo info) {
	ApplicationInfoPanel panel = createAboutPanel(title, info);
	panel.add(panel.createLabel(AndroidMessages.getDefault("info_label_url")), GridBagUtil.labelDefaults(0, 7));
	panel.add(panel.createText(WebserviceUrls.WEBSERVICE_URL), GridBagUtil.labelDefaults(0, 8));
	return panel;
    }

    private ApplicationInfoPanel createAboutPanel(String title, ApplicationInfo info) {
	ApplicationInfoPanel panel = new ApplicationInfoPanel(info);
	panel.setBorder(BorderFactory.createTitledBorder(title));
	return panel;
    }
}
